package com.fagejiang.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.config.JtaTransactionManagerFactoryBean;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * TODO
 *
 * @author 发哥讲Java
 * @version 1.0
 * @date 2021-03-10 9:31
 */
@Configuration
@MapperScan(basePackages = "com.fagejiang.mapper")
public class MybatisConfig {
    /**
     * 配置数据源
     * @return DataSource
     */
    @Bean
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://192.168.0.100:13306/zswz?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&tinyInt1isBit=false&useSSL=false\n");
        dataSource.setUsername("root");
        dataSource.setPassword("baian2020mql");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }

    /**
     * 解析 xml的 工具方法，借钱springboot的mybatis.mapper-locations注入的方法， 源码复制的，
     * @return  Resource
     */
    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<Resource> resources = new ArrayList<Resource>();
        Resource[] mappers = new Resource[0];
        try {
            mappers = resourceResolver.getResources("mappers/*.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        resources.addAll(Arrays.asList(mappers));
        return resources.toArray(new Resource[resources.size()]);
    }

    /**
     * 配置  SqlSessionFactory ，注入 数据源，映射的xml等信息
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setMapperLocations(resolveMapperLocations());
        Properties properties = new Properties();
        properties.setProperty("MySQL", "mysql");
        final VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        databaseIdProvider.setProperties(properties);
        factoryBean.setDatabaseIdProvider(databaseIdProvider);
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSession() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory(), ExecutorType.BATCH);
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}
